<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="hAdd(1,'0')">添加权限</el-button>
        </div>
        <el-table :data="list" border row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button v-if="row.type === 1" type="text" @click="hAdd(2,row.id)">添加</el-button>
              <el-button type="text" @click="hEdit(row)">编辑</el-button>
              <el-button type="text" @click="hDel(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 弹窗 -->
      <el-dialog
        v-if="showDialog"
        title="添加权限"
        :visible.sync="showDialog"
        close-on-click-modal
        close-on-press-escape
        @close="hClose"
      >
        <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" placeholder="请输入权限名称" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" placeholder="请输入权限标识" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="formData.description" placeholder="请输入权限描述" />
          </el-form-item>
          <el-form-item label="权限启用" prop="enVisible">
            <el-switch
              v-model="formData.enVisible"
              active-text="启用"
              active-value="1"
              inactive-text="不启用"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="hClose">取消</el-button>
            <el-button type="primary" @click="hSubmit">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { processingDepartments } from '@/utils'
import { addPermission, delPermission, getPermissionList, updatePermission } from '@/api/permissions'
export default {
  data() {
    return {
      list: [],
      showDialog: false,
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      },
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        enVisible: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ]
      },
      isEdit: false
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        console.log('获取权限数据', res)
        this.list = processingDepartments(res.data.data)
      } catch (err) {
        console.log(err)
      }
    },
    // 添加
    hAdd(type, id) {
      this.isEdit = false
      this.showDialog = true
      this.formData.pid = id
      this.formData.type = type
    },
    async doAdd() {
      const { data } = await addPermission(this.formData)
      console.log(data)
      this.loadPermissionList()
      this.hClose()
    },
    // 编辑
    hEdit(row) {
      this.isEdit = true
      this.showDialog = true
      this.formData = row
    },
    async doEdit() {
      const { data } = await updatePermission(this.formData.id)
      console.log(data)
      this.loadPermissionList()
      this.hClose()
    },
    // 确认
    hSubmit() {
      this.isEdit ? this.doEdit() : this.doAdd()
    },
    hDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const { data } = await delPermission(id)
        this.loadPermissionList()
        this.$message({
          type: 'success',
          message: data.message
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    hClose() {
      this.showDialog = false
      this.formData = {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      }
      this.$refs.formRef.resetFields()
    }
  }
}
</script>

<style>
</style>
